

package com.tjsiqi.service;

import com.tjsiqi.Vo.XhContractPhotoVo;
import com.tjsiqi.Vo.XhContractVo;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.XhContractMapper;
import com.tjsiqi.dao.XhContractPhotoMapper;
import com.tjsiqi.dto.XhContractDto;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.model.JbzContract;
import com.tjsiqi.model.XhContract;
import com.tjsiqi.model.XhContractEXP;
import com.tjsiqi.param.XhContractParam;
import com.tjsiqi.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SysXhContractService {
    @Autowired
    private XhContractMapper xhContractMapper;

    @Autowired
    private XhContractPhotoMapper xhContractPhotoMapper;


    //  查询合同备案
    public PageResult<XhContractVo> searchPageList(XhContractParam xhContractParam, PageQuery page) {

        XhContractDto xhContractDto = getXhContractDto(xhContractParam);
        List<XhContractVo> xhContractVoList = new ArrayList<>();

        int count = xhContractMapper.getXhContractCount(xhContractDto);
        System.out.println("xhContractVoCount"+count);
        if (count > 0){
            List<XhContract> xhContractList = xhContractMapper.getPageList(xhContractDto, page);
            for(XhContract xhContract:xhContractList){
                XhContractVo xhContractVo = new XhContractVo();
                List<XhContractPhotoVo> xhContractPhotoVoList = xhContractPhotoMapper.getList(xhContract.getId());
                xhContractVo.setImages(xhContractPhotoVoList);
                xhContractVo.setContractno(xhContract.getContractno());
                xhContractVo.setStatus(xhContract.getStatus());
                xhContractVo.setCheckunit(xhContract.getCheckunit());
                xhContractVo.setBuildunit(xhContract.getBuildunit());
                xhContractVo.setConstunit(xhContract.getConstunit());
                xhContractVo.setInputdate(xhContract.getInputdate());
                xhContractVo.setInputman(xhContract.getInputman());
                xhContractVo.setProjectdesc(xhContract.getProjectdesc());
                xhContractVo.setProjectname(xhContract.getProjectname());
                xhContractVo.setSigndate(xhContract.getSigndate());
                xhContractVoList.add(xhContractVo);
            }
            return PageResult.<XhContractVo>builder().total(count).data(xhContractVoList).build();
        }
        return PageResult.<XhContractVo>builder().build();
    }

//  封装查询数据
    private static XhContractDto getXhContractDto(XhContractParam xhContractParam){
        XhContractDto xhContractDto = new XhContractDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (StringUtils.isNotBlank(xhContractParam.getFromTime())) {
                xhContractDto.setFromTime(dateFormat.parse(xhContractParam.getFromTime()));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
                cal.add(Calendar.DATE, 0);
                cal.add(Calendar.MONTH,-3);
                xhContractDto.setFromTime(cal.getTime());
            }
            if (StringUtils.isNotBlank(xhContractParam.getToTime())) {
                xhContractDto.setToTime(dateFormat.parse(xhContractParam.getToTime()));
            }else{
                xhContractDto.setToTime(new Date());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题，正确格式为：yyyy-MM-dd");
        }

        System.out.println("获得默认开始日期"+DateTimeUtil.dateToStr(xhContractDto.getFromTime(),"yyyy-MM-dd"));

        xhContractDto.setJbzCode(xhContractParam.getJbzCode());

        if (StringUtils.isNotBlank(xhContractParam.getConstunit())){
            xhContractDto.setConstunit(xhContractParam.getConstunit());
        }else {
            xhContractDto.setConstunit(null);
        }

        if (StringUtils.isNotBlank(xhContractParam.getProjectname())){
            xhContractDto.setProjectname(xhContractParam.getProjectname());
        }else {
            xhContractDto.setProjectname(null);
        }

        return xhContractDto;
    }

    // 导出合同备案
    public List<XhContractEXP> exportByParam(XhContractParam xhContractParam) {

        XhContractDto xhContractDto = getXhContractDto(xhContractParam);

        List<XhContractEXP> xhContractEXPList = xhContractMapper.exportByParam(xhContractDto);

        return xhContractEXPList;
    }
}
